package modeller.databasedesignmodel.servicedemand.transaction.proceduralstatement;

import modeller.databasedesignmodel.relation.Relation;
import modeller.databasedesignmodel.servicedemand.IServiceDemand;
import modeller.databasedesignmodel.servicedemand.transaction.sqlquery.SQLQueryServiceDemand;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by:  Jason Ye
 * Date:        24/03/2012
 * Time:        10:30
 */
public class SQLQuerySequenceServiceDemand implements ProceduralStatementServiceDemand {

    HashMap<Relation,SQLQueryServiceDemand> newDemands = new HashMap<Relation, SQLQueryServiceDemand>();
    LinkedList<IServiceDemand> queryServiceDemands = new LinkedList<IServiceDemand>();

    @Override
    public String myToString() {

        StringBuffer ret = new StringBuffer("Sequence of SQL Statements - service demand:\n\t");

        for(Relation r : newDemands.keySet()){

            ret.append("\n\t" + newDemands.get(r).myToString());
        }

        return ret.toString();

    }

    @Override
    public double getServiceDemandForRelation(Relation relation) {
        double runningTotal = 0;
        for(IServiceDemand s : queryServiceDemands){
            runningTotal += s.getServiceDemandForRelation(relation);
        }
        return runningTotal;


    }



    public void addDemand(IServiceDemand dem) {

        queryServiceDemands.add(dem);
    }

    private void combineServiceDemands(Relation r, SQLQueryServiceDemand d) {

        SQLQueryServiceDemand old = newDemands.get(r);
        // sum together sqlsequenceservicedemands
        old.setServiceDemand(old.getServiceDemand()+d.getServiceDemand());

    }


}
